package com.iflytek.jobs.admin.controller;

import com.alibaba.excel.EasyExcel;
import com.iflytek.jobs.service.config.FollowRecordImportListener;
import com.iflytek.jobs.service.domain.vo.AllFollowRecordPojo;
import com.iflytek.jobs.service.domain.vo.FollowInfoExcel;
import com.iflytek.jobs.service.domain.vo.FollowRecordImportDTO;
import com.iflytek.jobs.service.domain.vo.FollowRecordPojo;
import com.iflytek.jobs.service.service.IFollowRecordService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 实习跟踪 回访记录
 */
@RestController
@RequestMapping("/jobs/follow")
public class FollowRecordController extends BaseController {
    @Autowired
    private IFollowRecordService followRecordService;
    @Autowired
    private FollowRecordImportListener importListener;
    /**
     * 根据账号查询学生是否存在
     * @param account 账号
     * @return 学生名字昵称
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,common,prof,emp,temp')")
    @Log(title = "获取学生昵称名字")
    @PostMapping("/queryStudent")
    public AjaxResult queryStudent(@RequestParam("account") String account) {
        List<String> nickName=followRecordService.queryStudent(account);
        if (nickName.isEmpty()){
            return AjaxResult.warn("学生不存在");
        }
        return AjaxResult.success("查询成功",nickName);
    }
    /**
     * 添加回访记录
     * @param followRecordPojo 回访记录
     * @return 成功 | 失败
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,common,prof,emp,temp')")
    @Log(title = "添加回访记录")
    @PostMapping("/addFollowRecord")
    public AjaxResult addFollowRecord(@RequestBody FollowRecordPojo followRecordPojo) {
        // 获取负责老师账号
        String teacherAccount= SecurityUtils.getUsername();
        followRecordPojo.setTeacherAccount(teacherAccount);
        // 查看是否重复添加回访次数  true 没有重复 false 有重复
        if(followRecordService.repeatFollow(followRecordPojo)){

            // 查看是否有这个学生
            List<String> nickName=followRecordService.queryStudent(followRecordPojo.getStudentAccount());
            if (nickName.isEmpty()){
                return AjaxResult.warn("学生不存在");
            }else {
                if (followRecordService.addFollowRecord(followRecordPojo)){
                    return AjaxResult.success("添加成功","添加成功");
                }
                return AjaxResult.error("添加失败,异常","添加失败,异常");
            }
        }else {
            return AjaxResult.error("添加失败：重复回访次数添加","添加失败：重复回访次数添加");
        }

    }


    /**
     * 查询当前老师的所有回访记录
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,common,prof,emp,temp')")
    @Log(title = "获取所有回访记录")
    @PostMapping("/queryAllFollowRecord")
    public TableDataInfo queryAllFollowRecord(@RequestBody FollowRecordPojo followRecordPojo) {
        startPage(true);
        String teacherAccount= SecurityUtils.getUsername();
        followRecordPojo.setTeacherAccount(teacherAccount);
        List<AllFollowRecordPojo> allFollowRecordPojoList=followRecordService.queryAllFollowRecord(followRecordPojo);
        return getDataTable(allFollowRecordPojoList);
    }


    /**
     * 获取详情页信息
     * @param followRecordPojo 回访记录
     * @return 详情页信息
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,common,prof,emp,temp')")
    @Log(title = "获取详情页信息")
    @PostMapping("/getDetailData")
    public AjaxResult getDetailData(@RequestBody FollowRecordPojo followRecordPojo) {
        // 获取指导老师
        String teacherAccount= SecurityUtils.getUsername();

        followRecordPojo.setTeacherAccount(teacherAccount);
        List<FollowRecordPojo> followRecordPojos=followRecordService.getDetailData(followRecordPojo);
        if (!followRecordPojos.isEmpty()){

            return AjaxResult.success().put("data",followRecordPojos.get(0));
        }
        else {
            return AjaxResult.error("详情获取失败,异常","详情获取失败,异常");
        }
    }


    /**
     * 获取要编辑修改的回访记录
     * @param followRecordPojo 回访记录
     * @return 要编辑修改的回访记录
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,common,prof,emp,temp')")
    @Log(title = "获取要编辑修改的回访记录")
    @PostMapping("/editData")
    public AjaxResult getEditData(@RequestBody FollowRecordPojo followRecordPojo) {
        // 获取指导老师
        String teacherAccount= SecurityUtils.getUsername();
        followRecordPojo.setTeacherAccount(teacherAccount);
        List<FollowRecordPojo> followRecordPojos=followRecordService.getEditData(followRecordPojo);
        if (!followRecordPojos.isEmpty()){
            return AjaxResult.success().put("data",followRecordPojos.get(0));
        }else {
            return AjaxResult.error("编辑获取失败,异常","编辑获取失败,异常");



        }
    }


    /**
     * 保存修改
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,common,prof,emp,temp')")
    @Log(title = "保存修改")
    @PostMapping("/saveChanges")
    public AjaxResult  saveChanges(@RequestBody FollowRecordPojo followRecordPojo) {
        // 获取指导老师
        String teacherAccount= SecurityUtils.getUsername();
        followRecordPojo.setTeacherAccount(teacherAccount);
        if(followRecordService.saveChanges(followRecordPojo)){
            return AjaxResult.success("修改成功","修改成功");
        }else {
            return AjaxResult.error("修改失败,异常","修改失败,异常");
        }
    }


    /**
     * 删除一条记录的
     * @param followRecordPojo 回访记录
     * @return 删除成功 | 删除失败
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,common,prof,emp,temp')")
    @Log(title = "删除一条记录")
    @PostMapping("/deleteRecord")
    public AjaxResult deleteRecord(@RequestBody FollowRecordPojo followRecordPojo) {
        String teacherAccount= SecurityUtils.getUsername();
        followRecordPojo.setTeacherAccount(teacherAccount);
        if (followRecordService.deleteRecord(followRecordPojo)){
            return AjaxResult.success("删除成功","删除成功");
        }
        else {
            return AjaxResult.error("删除失败,异常","删除失败,异常");
        }
    }


    /**
     * 对比多个记录
     * @param followRecordPojoList 回访记录
     * @return 对比结果
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,common,prof,emp,temp')")
    @Log(title = "对比多个记录")
    @PostMapping("/compareRecord")
    public AjaxResult compareRecord(@RequestBody List<FollowRecordPojo> followRecordPojoList) {
        String teacherAccount= SecurityUtils.getUsername();
        List<FollowRecordPojo> recordPojos=new ArrayList<>();
        for (FollowRecordPojo followRecordPojo:followRecordPojoList){
            followRecordPojo.setTeacherAccount(teacherAccount);
                recordPojos.add(followRecordService.compareRecord(followRecordPojo));
        }
        return AjaxResult.success().put("data",recordPojos);
    }


    /**
     * 获取excel下载
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,common,prof,emp,temp')")
    @Log(title = "获取excel下载")
    @PostMapping("/excelFollowRecord")
    public void excelFollowRecord(@RequestBody FollowRecordPojo followRecordPojo, HttpServletResponse response) {
        // 全部
        if (followRecordPojo.getInternExport()){
            List<FollowInfoExcel> followInfoExcelList=followRecordService.excelFollowRecord(followRecordPojo);
            ExcelUtil<FollowInfoExcel> util=new ExcelUtil<>(FollowInfoExcel.class);
            util.exportExcel(response, followInfoExcelList, "实习跟踪回访记录");
        }else {
            // 指定负责老师
            String teacherAccount= SecurityUtils.getUsername();
            followRecordPojo.setTeacherAccount(teacherAccount);
            List<FollowInfoExcel> followRecordPojos=followRecordService.excelFollowRecord(followRecordPojo);
            ExcelUtil<FollowInfoExcel> util=new ExcelUtil<>(FollowInfoExcel.class);
            util.exportExcel(response, followRecordPojos, "实习跟踪回访记录");
        }
    }


    /**
     * 批量导入Excel回访记录
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,common,prof,emp,temp')")
    @Log(title = "批量导入Excel回访记录")
    @PostMapping("/importExcel")
    public AjaxResult importExcel(MultipartFile file) {
        try {
            // 调用EasyExcel解析文件，指定监听器
            EasyExcel.read(file.getInputStream(), FollowRecordImportDTO.class, importListener)
                    .sheet() // 读取第一个工作表
                    .doRead(); // 开始解析
            return AjaxResult.success("导入成功", null);
        } catch (IOException e) {
            return AjaxResult.error("导入失败，文件读取异常");
        } catch (RuntimeException e) {
            return AjaxResult.error("导入失败：" + e.getMessage());
        }
    }

}
